package com.demo.apply.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.demo.apply.common.PageInfoBT;
import com.demo.apply.core.common.constant.factory.PageFactory;
import com.demo.apply.core.common.constant.state.BizLogType;
import com.demo.apply.model.User;
import com.demo.apply.warpper.TDPapplyWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.demo.apply.model.TDApply;
import com.demo.apply.service.ITDApplyService;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2021-11-10 09:50:37
 */
@Controller
public class TDApplyController extends BaseController {


    @Autowired
    private ITDApplyService tDApplyService;

    /**
     * 跳转到登录页面
     */
    @RequestMapping("")
    public String toLogin() {
        return "login.html";
    }
    /**
     * 跳转到列表页面
     */
    @RequestMapping("log")
    public String index() {
        return "log/log.html";
    }

    /**
     * 登录验证
     */
    @RequestMapping("login")
    public String login(String username, String password, Model model, HttpSession session) {
        User user=tDApplyService.check(username,password);
        if(user==null){
            model.addAttribute("tips","账号或密码错误");
            return "redirect:/";
        }else {
//            把用户信息放到session中
            session.setAttribute("user",user);
            return "tDApply.html";
        }

    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/tDApply_add")
    public String tDApplyAdd(Model model,HttpSession session) {
        User user= (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "tDApply_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/tDApply_update/{tDApplyId}")
    public String tDApplyUpdate(@PathVariable Integer tDApplyId, Model model) {
        TDApply tDApply = tDApplyService.selectById(tDApplyId);
        Date createtime = tDApply.getCreatetime();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String createTimeStr=simpleDateFormat.format(createtime);
        model.addAttribute("createTime",createTimeStr);
        model.addAttribute("item",tDApply);
        return "tDApply_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(TDApply tdApply) {
        System.out.println(tdApply);
        List<Map<String, Object>> list = this.tDApplyService.list(tdApply);
        return super.warpObject(new TDPapplyWarpper(list));
    }


    /**
     * 查询操作日志列表
     */
    @RequestMapping("/getPageList")
    @ResponseBody
    public Object getPagelist(@RequestParam(required = false) String applyName, @RequestParam(required = false) String applySerial, @RequestParam(required = false) String count, @RequestParam(required = false) String money,@RequestParam(required = false) String type) {
        Page<TDApply> page = new PageFactory<TDApply>().defaultPage();
        List<Map<String, Object>> result = tDApplyService.getPageList(page, applyName, applySerial, count, money,type, page.getOrderByField(), page.isAsc());
        page.setRecords(new TDPapplyWarpper(result).wrap());
        return new PageInfoBT<>(page);
    }
    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TDApply tDApply) {
        tDApplyService.insert(tDApply);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tDApplyId) {
        tDApplyService.deleteById(tDApplyId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TDApply tDApply) {
        tDApplyService.updateById(tDApply);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{tDApplyId}")
    @ResponseBody
    public Object detail(@PathVariable("tDApplyId") Integer tDApplyId) {
        return tDApplyService.selectById(tDApplyId);
    }

    @RequestMapping("layuiList")
    public String layuiList(){
        return "layuiList.html";
    }

    @RequestMapping("echarts")
    public String echarts(){
        return "echarts.html";
    }
}
